import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import login from '@/views/login/Login.vue'
import main from '@/views/Main.vue'
import HomeView from '@/views/HomeView.vue';
// import main from '@/views/main/main.vue'
// import { routes } from 'vue-router/auto-routes'
// const router = createRouter({
//   history: createWebHistory(),
//   routes: routes, //使用自动路由
// })
//!定义路由表
export const routes: Array<RouteRecordRaw> = [
  {
    path: '/Login',
    name: 'login',
    component: () => import('@/views/login/Login.vue'),
  },
  {
    path: '/',
    name: 'main',
    component: () => import('@/views/Main.vue'),
    meta: { title: '主页' },
    children: [
      {
        path: '/HomeView',
        name: 'HomeView',
        component: () => import('@/views/HomeView.vue'),
        meta: { title: '系统管理' },
        children: [
          {
            path: 'cyara',
            name: 'cyara',
            component: () => import('@/views/main/cyara.vue'),
            meta: { title: '角色管理' },
          },
        ],
      },
      {
        path: '/animetion',
        name: 'animetion',
        component: () => import('@/views/animetion.vue'),
        meta: { title: '游戏' },
      },

    ]
  },
];

//!使用展开运算符管理路由表

export const routerList = [
  ...routes
]

//!注册路由
const router = createRouter({
  // history:createWebHashHistory(), //hash模式
  history: createWebHistory(import.meta.env.BASE_URL), //history模式
  routes: routerList,
});

// 配置全局守卫
router.beforeEach((to, from, next) => {
  const istrue = localStorage.getItem('token')
  if (istrue) {
    //已经登录
    if (to.path === '/Login') {
      next()
    } else {
      next()
    }
  } else {
    //没有登录情况
    if (to.path === '/Login') {
      next()
    } else {
      alert('未登录，请先登录账号')
      next('/Login')
    }
  }
})
export default router
